package com.gsi.webstore.platform.storefront.internal.calculation.rule;

import com.intershop.beehive.bts.capi.orderprocess.LineItemCtnr;
import com.intershop.beehive.bts.capi.orderprocess.ProductLineItem;
import com.intershop.beehive.bts.capi.orderprocess.calculation.ComputedProductLineItem;
import com.intershop.beehive.bts.capi.orderprocess.calculation.rules.CalculateSingleProductBasePricesRule;
import com.intershop.component.foundation.capi.spreadsheet.CalculationRule;
import com.intershop.component.foundation.capi.spreadsheet.ComputedMoneyItem;
import com.intershop.component.foundation.capi.spreadsheet.items.ComputedMoneyItemImpl;

public class CustomCalculateSingleProductBasePricesRule extends CalculateSingleProductBasePricesRule
{
    private static final String IS_GIFT_CARD_ATTRIBUTE = "isGiftCard";
    private static final String IS_GIFT_CARD_VIRTUAL_ATTRIBUTE_KEY = "isGiftCardVirtual";
    public CustomCalculateSingleProductBasePricesRule(GroupOut<ComputedMoneyItem> groupOut, GroupIn<ComputedProductLineItem> groupIn,
                    boolean addNAPrices)
    {
        super(groupOut, groupIn, addNAPrices);
    }
    
    @Override
    protected ComputedMoneyItem createTargetItem(String targetItemGroup, String targetItemID, ComputedProductLineItem sourceItem, CalculationRule<ComputedMoneyItem, ComputedProductLineItem, LineItemCtnr>.Context context){
        ComputedMoneyItem computedItem = super.createTargetItem(targetItemGroup, targetItemID, sourceItem, context);
        // override price of gift card
        
        ProductLineItem pli = sourceItem.getProductLineItem();
            if (pli.containsAttribute(IS_GIFT_CARD_ATTRIBUTE) && pli.containsAttribute(IS_GIFT_CARD_VIRTUAL_ATTRIBUTE_KEY)){
                if(computedItem == null)
                {
                    computedItem = new ComputedMoneyItemImpl(targetItemGroup, targetItemID);
                }
                ((ComputedMoneyItemImpl)computedItem).setMoney(sourceItem.getProductLineItem().getSingleBasePricePC());
            } 

        return computedItem;
    }

}